package com.orangelife.mobile.property.biz;

import android.os.Handler;

import com.orangelife.mobile.property.bean.PropertyBillHistoryListEntity;
import com.orangelife.mobile.property.bean.PropertyFeeListEntity;
import com.orangelife.mobile.constants.PropertyURL;
import com.curry.android.http.OrangeDataHandler;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class PropertyHttpBiz {
    /** 查询房屋可抵扣金额 */
    public static void getDeductionMoney(String commID, String apartmentID, VolleyHttpUtil.ResponseCallBack callBack) {
        HashMap<String, String> urlMap = new HashMap<>();
        urlMap.put("commID", String.valueOf(commID));
        urlMap.put("apartmentID", String.valueOf(apartmentID));
        VolleyHttpUtil.getInstance().getWithCallback(PropertyURL.DEDUCTION_MONEY, urlMap, callBack);
    }

    public static String handleDeductionMoney(JSONObject response) {
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            Map<String, Object> map = OrangeDataHandler.getMapWithEntity(response);
            if (map != null) {
                return String.valueOf(map.get("money"));
            }
        }
        return null;
    }

    /**
     * 是否是新版的缴费系统
     *
     * @param billType 1=旧的，2=新的
     * @return
     */
    public static boolean isNewBillType(String billType) {
        return "2".equals(billType);
    }

    /**
     * 由于新物业缴费系统只在部分小区推行，其他小区沿用旧系统。故需要判断小区使用的是何种缴费系统。
     * （在选择房屋来查询详情时使用）
     */
    public static void getPropertyType(String commID, VolleyHttpUtil.ResponseCallBack callBack) {
        HashMap<String, String> urlMap = new HashMap<>();
        urlMap.put("commID", String.valueOf(commID));
        VolleyHttpUtil.getInstance().getWithCallback(PropertyURL.TYPE_NEW_OLD, urlMap, callBack);
    }


    public static String handlePropertyType(JSONObject response) {
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            Map<String, Object> map = OrangeDataHandler.getMapWithEntity(response);
            if (map != null) {
                return String.valueOf(map.get("billType"));
            }
        }
        return null;
    }

    /** 请求物业缴费的房屋信息列表 */
    public static void getFeeList(int pageNo, int pageSize, VolleyHttpUtil.ResponseCallBack callBack) {
        VolleyHttpUtil.getInstance().cancel("PROPERTY_FEE_LIST");
        HashMap<String, String> urlMap = new HashMap<>();
        urlMap.put("pageIndex", String.valueOf(pageNo));
        urlMap.put("pageSize", String.valueOf(pageSize));
//        urlMap.put("commID ", CommSp2.getCommID());
//        urlMap.put("apartmentID", "a0abfa05-3a4d-4ab3-9a25-5d58509b4199");
        VolleyHttpUtil.getInstance().getWithCallback(PropertyURL.FEE_LIST, urlMap, callBack, "PROPERTY_FEE_LIST");
    }

    /** 获取物业缴费的房屋信息列表 */
    public static List<PropertyFeeListEntity> handleFeeList(JSONObject response) {
        if (com.curry.android.http.OrangeErrorHandler.getInstance().isSuccess(response)) {
            return OrangeDataHandler.getListFromJsonWithPageEntity(response, new TypeToken<List<PropertyFeeListEntity>>() {
            }.getType(), null, null);
        }
        return null;
    }

    /** 请求应缴账单信息 */
    public static void getFeeDetail(String commID, String apartmentID, String type, Handler handler, int what) {
        HashMap<String, String> urlMap = new HashMap<>();
        urlMap.put("apartmentID", apartmentID);
        urlMap.put("commID", commID);
        // TODO: 2016/5/23 将删除这个参数
        //0:不检查是否存在关联信息,1:检查是否存在关联信息
        urlMap.put("type", type);
        VolleyHttpUtil.getInstance().getWithHandler(PropertyURL.FEE_DETAIL, handler, what, urlMap);
    }

    /** 处理应缴账单信息 */
    public static Map<String, Object> handleFeeDetail(Object obj) {
        try {
            JSONObject jo = new JSONObject(obj.toString());
            if (com.curry.android.http.OrangeErrorHandler.getInstance().isSuccess(jo)) {
                return OrangeDataHandler.getMapWithEntityNoGson(jo);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /** 获取物业缴费历史记录列表 */
    public static void getFeeHistoryList(int pageNo, int pageSize, VolleyHttpUtil.ResponseCallBack callBack) {
        VolleyHttpUtil.getInstance().cancel("PROPERTY_HISTORY_LIST");
        HashMap<String, String> urlMap = new HashMap<>();
        urlMap.put("pageIndex", String.valueOf(pageNo));
        urlMap.put("pageSize", String.valueOf(pageSize));
        VolleyHttpUtil.getInstance().getWithCallback(PropertyURL.HISTORY_LIST, urlMap, callBack, "PROPERTY_HISTORY_LIST");
    }

    /** 处理物业缴费历史记录列表 */
    public static List<PropertyBillHistoryListEntity> handleFeeHistoryList(JSONObject response) {
        if (com.curry.android.http.OrangeErrorHandler.getInstance().isSuccess(response)) {
            List<PropertyBillHistoryListEntity> list = OrangeDataHandler.getListFromJsonWithPageEntity(response, new TypeToken<List<PropertyBillHistoryListEntity>>() {
            }.getType(), null, null);
            return list;
        }
        return null;
    }

    /** 删除物业缴费列表项 */
    public static void delFee(VolleyHttpUtil.ResponseCallBack callBack, String commID, String apartmentID) {
        HashMap<String, String> map = new HashMap<>();
        map.put("commID", commID);
        map.put("apartmentID", apartmentID);
        VolleyHttpUtil.getInstance().delWithCallback(PropertyURL.DELETE_FEE, map, callBack);
    }

    /** 历史缴费列表中，点击item，获取子项的流水号 */
    public static void getOutTradeNoInHistoryList(String commID, String payBillID, VolleyHttpUtil.ResponseCallBack callBack) {
        HashMap<String, String> urlMap = new HashMap<>();
        urlMap.put("commID", commID);
        urlMap.put("orderID", payBillID);
        VolleyHttpUtil.getInstance().getWithCallback(PropertyURL.OUT_TRADE_NO, urlMap, callBack);
    }

    /** 支付成功之后，更新物业缴费账单的状态，未支付转为支付中  payBillID 就是 orderID */
    public static void updatePayBill(String orderID, VolleyHttpUtil.ResponseCallBack callback) {
        HashMap<String, Object> urlMap = new HashMap<>();
        urlMap.put("orderID", orderID);
        VolleyHttpUtil.getInstance().putWithCallback(PropertyURL.UPDATE_PAY_BILL, urlMap, callback);
    }

    /** 获取物业缴费历史记录详情 */
    public static void getFeeHistoryDetail(String commID, String apartmentID, String orderID, VolleyHttpUtil.ResponseCallBack callBack) {
        HashMap<String, String> urlMap = new HashMap<>();
        urlMap.put("apartmentID", apartmentID);
        urlMap.put("commID", commID);
        //0:不检查是否存在关联信息,1:检查是否存在关联信息
        urlMap.put("orderID", orderID);
        VolleyHttpUtil.getInstance().getWithCallback(PropertyURL.HISTORY_DETAIL, urlMap, callBack);
    }
}
